package com.education.user.mapper;

import com.education.common.pojo.userData;
import com.education.user.pojo.SysUser;
import com.education.user.pojo.SysUserVO;
import com.education.user.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

@Repository
public interface sysUserMapper extends Mapper<SysUser> {

    @Update({
            "   update educationlessons.t_teacher" +
//            "    <trim prefix=\"SET\" suffixOverrides=\",\">\n" +
                    "    SET    " +
                    "    teacher_image = #{sysIco}" +
//            "    </trim>\n" +
                    "    where user_id = #{id}"
    })
    Integer updateImgForTeacherTable(SysUser user);
    
    @Select({
        "<script>",
            "  SELECT id, sys_relname sysRelname,  sys_ico sysIco, sys_state sysState, sys_phone sysPhone, sys_address sysAddress, create_time createTime, sys_type sysType, sys_attention SysAttention, school_id schoolId, sys_sex sysSex, sys_age sysAge,customer_type customerType  from sys_user",
            "    <where>",
            "       <if test=\"sysRelname != null  and sysRelname != '' \"> and sys_relname = #{sysRelname}</if>",
            "       <if test=\"sysState != null \"> and sys_state = #{sysState}</if>",
            "       <if test=\"sysPhone != null  and sysPhone != '' \"> and sys_phone = #{sysPhone}</if>",
            "       <if test=\"createTime != null \"> and create_time = #{createTime}</if>",
            "       <if test=\"startTime != null  and startTime != '' \"> and create_time > #{startTime}</if>",
            "       <if test=\"endTime != null  and endTime != '' \"> and  #{endTime} > create_time </if>",
            "       <if test=\"roleIdList != null \"> and id in",
            "           <foreach item='id' collection='roleIdList' separator=',' open='(' close=')' index=''>",
            "               #{id}",
            "           </foreach>",
            "       </if>",
            "    </where>",
        "</script>"
    })
    List<SysUser> listOther(Map<String,Object> map);


    /**
     *  进行关联查询 角色名称 和 部门名称
     */
    @Select({
            "<script>SELECT\n" +
                    "\tsu.sys_relname AS sysRelname,\n" +
                    "\tsu.sys_ico AS sysIco,\n" +
                    "\tsu.sys_phone AS sysPhone,\n" +
                    "\tsu.id AS id,\n" +
                    "\tsu.sys_sex AS sysSex,\n" +
                    "\tsu.sys_age AS sysAge,\n" +
                    "\tsr.role_name AS roleName,\n" +
                    "\tsu.sys_state AS sysState,\n" +
                    "\tsu.subjects,\n" +
                    "\tsu.create_time AS createTime,\n" +
                    "\tsu.dept_id AS deptId,\n" +
                    "\tsu.usb_key AS usbKey, \n" +
                    "\tsu.school_id AS schoolId, \n" +
                    "\tsu.sys_type AS sysType \n" +
                    "FROM\n" +
                    "\tsys_user su\n" +
                    "\tLEFT JOIN sys_user_role sur ON su.id = sur.user_id\n" +
                    "\tLEFT JOIN sys_role sr ON sr.role_id = sur.role_id\n" +
                    "WHERE\n" +
                    "\t1=1 and id != '1'\n" +
                    "<if test=\"roleName != null  and roleName != '' \"> and sr.role_name = #{roleName}</if>" +
                    "<if test=\"sysPhone != null  and sysPhone != '' \"> and su.sys_phone like concat('%',#{sysPhone},'%') </if>" +
                    "<if test=\"schoolId != null  and schoolId != '' \"> and su.school_id = #{schoolId}</if>" +
                    "ORDER BY\n" +
                    "\tsu.create_time " +
                    "</script>"
    })
    List<SysUserVO> selectUserList(SysUser user);


    /**
     *  进行关联查询 角色名称 和 部门名称
     */
    @Select({
            "<script>",
            "  SELECT " +
                    "sys_relname AS sysRelname," +
                    "sys_ico AS sysIco," +
                    "sys_phone AS sysPhone," +
                    "id AS id," +
                    "sys_sex AS sysSex," +
                    "sys_age AS sysAge," +
                    "subjects" +
                    " from " +
                    "sys_user",
            "    <where>",
            "       <if test=\"schoolId != null  and schoolId != '' \"> and school_id = #{schoolId}</if>",
            "       <if test=\"sysPhone != null  and sysPhone != '' \"> and sys_phone = #{sysPhone}</if>",
            "       and sys_state = '0' ",
            "    </where>",
            "</script>"
    })
    SysUser selectUserListFeign(SysUser user);


    /**
     *  进行关联查询 角色名称 和 部门名称
     */
    @Select({
            "<script>",
            "  SELECT " +
                    " su.id  as value, su.sys_relname as label " +
                    "from " +
                    "sys_user su",
            "        LEFT JOIN sys_role sr ON sr.role_id = ( SELECT role_id FROM sys_user_role sur WHERE sur.user_id = su.id )",
            "    <where>",
            "        and sr.role_name = '老师' ",
            "       <if test=\"deptId != null  and deptId != '' \"> and su.dept_id = #{deptId} </if> ",
            "       <if test=\"id != null  and id != '' \"> and su.id != #{id} </if> ",
            "    </where>",
            "</script>"
    })
    List<SysUser> selectUserListRoom(SysUser user);


    /**
     *  查询讲授人
     */
    @Select({
            "<script>",
            "  SELECT " +
                    "su.sys_relname as sysRelname, su.id as id " +
                    "from " +
                    "sys_user su",
            "    <where>",
            "       <if test=\"schoolId != null  and schoolId != '' \"> and school_id = #{schoolId}</if>",
            "       and su.id in ( select user_id from sys_user_role where role_id = 2 )",
            "    </where>",
            "</script>"
    })
    List<SysUser> selectUserListOther(SysUser user);


    /** 原sysAppLogin 的逻辑代码 */
    @Select({
            "<script>",
                    "SELECT * from  sys_user " ,
                "<where>",
                "School_Id = #{schoolId}" +
                "<if test=\"sysType != null and sysType != ''\"> AND sys_ype =#{sysType}</if>" ,
                "<if test=\"customerType != null  and customerType != ''\"> AND customer_type =#{customerType}</if>",
                "</where>",
            "</script>"
    })
    SysUser selectByDeptId(userData data);


    /** 查询详情 */
    @Select({
            "<script>",
            "  SELECT " +
                    "su.id,su.price,su.usb_key usbKey, su.customer_type customerType,sys_password sysPassword, su.sys_relname sysRelname,  su.sys_ico sysIco, su.sys_state sysState, su.sys_phone sysPhone, su.sys_address sysAddress, su.create_time createTime, su.sys_type sysType, su.sys_attention SysAttention, su.school_id schoolId,su.dept_id deptId, su.sys_sex sysSex, su.sys_age sysAge, su.class_id classId, sr.role_id as roleId ,sr.role_name as roleName,su.expire_date expireDate " +                    "from " +
                    " sys_user su ",
            "        LEFT JOIN sys_role sr ON sr.role_id = ( SELECT role_id FROM sys_user_role sur WHERE sur.user_id = su.id ) ",
            "    <where>",
            "       <if test=\"id != null  \"> and su.id = #{id}</if>",
            "       <if test=\"strId != null  \"> and su.id = #{strId}</if>",
            "       <if test=\"sysPhone != null  \"> and su.sys_phone = #{sysPhone}</if>",
            "    </where>",
            "</script>"
    })
    SysUser selectOneUser(SysUser user);

    @Select({"SELECT SUM(price) FROM `sys_user` WHERE sys_phone LIKE concat('%',#{sysPhone},'%')"})
    Integer selectSumPrice(SysUser user);

    @Select({"SELECT\n" +
            "\tid,\n" +
            "\tsys_relname sysRelname,\n" +
            "\tsys_password sysPassword,\n" +
            "\tsys_ico sysIco,\n" +
            "\tsys_state sysState,\n" +
            "\tsys_phone sysPhone,\n" +
            "\tsys_address sysAddress,\n" +
            "\tcreate_time createTime,\n" +
            "\tsys_type sysType,\n" +
            "\tschool_id schoolId,\n" +
            "\tsys_sex sysSex,\n" +
            "\tprice price,\n" +
            "\tsys_age sysAge,\n" +
            "\tlogins logins,\n" +
            "\tsys_attention SysAttention,\n" +
            "\tdept_id deptId\n" +
            "FROM\n" +
            "\tsys_user WHERE sys_phone LIKE concat('%',#{sysPhone},'%')"})
    List<SysUser> findByLikePhone(SysUser user);


    @Update({
            "   update sys_user\n" +
//            "    <trim prefix=\"SET\" suffixOverrides=\",\">\n" +
            "    SET    \n" +
            "    sys_password = #{sysPassword}\n" +
//            "    </trim>\n" +
            "    where sys_phone = #{sysPhone}"
    })
    int updatePassword(SysUser user);

    @Select({"SELECT *,sys_password as sysPassword FROM `sys_user` WHERE id = #{userId}"})
    User selectUserById(Integer userId);

    @Update({
            "   update sys_user\n" +
//            "    <trim prefix=\"SET\" suffixOverrides=\",\">\n" +
                    "    SET    \n" +
                    "    sys_password = #{sysPassword}\n" +
//            "    </trim>\n" +
                    "    where id = #{id}"
    })
    Integer updateNewPassword(User user);
}
